﻿
//加号加入购物车
index_changenums = function (p_id, price, cur) {
   
    GoodsNumber(p_id, function (a) {
        index_change(p_id, price, cur, a)
    })
}

function index_change(p_id, price, cur, num) {
    //1.改变显示数量
    var Key = "";
    var data = "";
    var GoodsNum = parseInt(num);

    for (var i = 0; i < localStorage.length; i++) {
        Key = localStorage.key(i);
        if (Key.substring(0, 5) == "good_") {
            getData = JSON.parse(xxl.xd.getValue(Key));
            if (getData.MicroGoodsID == p_id) { $("#number_" + p_id).val(getData.ConvertNumber); }
        }
    }

    var number = parseInt($("#number_" + p_id).val());
    var old_number = number;
    if (cur == "+") {
        if (GoodsNum == 0 || GoodsNum < (number + 1)) {
            alert("库存不足");
            return;
        }
        else {
            number++;
        }
    } else {
        number < 1 ? 0 : number--;
        if (number < 1) {
            $("#botton_" + p_id).css("display", "block");
            $("#botton_" + p_id).next(".d_1").css("display", "none");
        }
    }
    $("#number_" + p_id).val(number);

    //2.操作cookie记录购物车商品数据
    if (cur == "+" && parseInt(GoodsNum) >= number) {
        var strKey = "";
        var txtGoodID = p_id;
        var getData = "";
        var total_num = 0;
        var total_price = 0;
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                if (getData.MicroGoodsID == p_id) { break; } else { getData = ""; }
            }
        }
        if (getData == "") {//新增
            var goodlist = new Object();
            goodlist.Type = "good_" + retRndNum(3);
            goodlist.MicroGoodsID = txtGoodID;
            goodlist.ConvertNumber = number;
            goodlist.sumprice = (parseFloat(price) * parseInt(number)).toFixed(2);
            xxl.xd.setValue(goodlist.Type, JSON.stringify(goodlist));
        } else { //编辑
            getData.ConvertNumber = parseInt(getData.ConvertNumber) + 1;
            getData.sumprice = (parseFloat(price) * parseInt(getData.ConvertNumber)).toFixed(2);
            xxl.xd.setValue(getData.Type, JSON.stringify(getData));
        }
        var cookieStr = "";
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                cookieStr += getData.MicroGoodsID + ',' + getData.sumprice + ',' + getData.ConvertNumber + '|';
                $("number_" + p_id).val(getData.ConvertNumber);
                total_num += parseInt(getData.ConvertNumber);
                total_price += parseFloat(getData.sumprice);
            }
        }

    }
    else if (cur == "-" && old_number > 0) {
        var strKey = "";
        var txtGoodID = p_id;
        var getData = "";
        var total_num = 0;
        var total_price = 0;
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                if (getData.MicroGoodsID == p_id) { break; } else { getData = ""; }
            }
        }
        if (getData != "") { //减少
            getData.ConvertNumber = parseInt(getData.ConvertNumber) - 1;
            getData.sumprice = (parseFloat(price) * parseInt(getData.ConvertNumber)).toFixed(2);
            xxl.xd.setValue(getData.Type, JSON.stringify(getData));
        }
        var cookieStr = "";
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                cookieStr += getData.MicroGoodsID + ',' + getData.sumprice + ',' + getData.ConvertNumber + '|';
                $("number_" + p_id).val(getData.ConvertNumber);
                total_num += parseInt(getData.ConvertNumber);
                total_price += parseFloat(getData.sumprice);
            }
        }

    }
    setCookie("goodlist", cookieStr);
    setCookie("goodlist_num", total_num);
    setCookie("goodlist_price", parseFloat(total_price).toFixed(2));
   
    $("#icon_num").text(getCookie("goodlist_num"));
    $("#icon_price").text(getCookie("goodlist_price"));
    if (getCookie("goodlist_num") > 0) {
        $("#icon_num").css("display", "block");
        $("#icon_price").css("display", "block");
    }
    else {
        $("#icon_num").css("display", "none");
        $("#icon_price").css("display", "none");
    }
}

function setck(p_id, cur, GoodsNum, number, price, old_number) {


    if (cur == "+" && parseInt(GoodsNum) >= number) {
        var strKey = "";
        var txtGoodID = p_id;
        var getData = "";
        var total_num = 0;
        var total_price = 0;
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                if (getData.MicroGoodsID == p_id) { break; } else { getData = ""; }
            }
        }
        if (getData == "") {//新增
            var goodlist = new Object();
            goodlist.Type = "good_" + retRndNum(3);
            goodlist.MicroGoodsID = txtGoodID;
            goodlist.ConvertNumber = old_number;
            goodlist.sumprice = (parseFloat(price) * parseInt(old_number)).toFixed(2);
            xxl.xd.setValue(goodlist.Type, JSON.stringify(goodlist));
        } else { //编辑
            getData.ConvertNumber = parseInt(getData.ConvertNumber) + 1;
            getData.sumprice = (parseFloat(price) * parseInt(getData.ConvertNumber)).toFixed(2);
            xxl.xd.setValue(getData.Type, JSON.stringify(getData));
        }
        var cookieStr = "";
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                cookieStr += getData.MicroGoodsID + ',' + getData.sumprice + ',' + getData.ConvertNumber + '|';
                $("number_" + p_id).val(getData.ConvertNumber);
                total_num += parseInt(getData.ConvertNumber);
                total_price += parseFloat(getData.sumprice);
            }
        }

    }
    else if (cur == "-" && old_number > 0) {

        var strKey = "";
        var txtGoodID = p_id;
        var getData = "";
        var total_num = 0;
        var total_price = 0;
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));

                if (getData.MicroGoodsID == p_id) { break; } else { getData = ""; }
            }
        }
        if (getData != "") { //减少
            if (parseInt(getData.ConvertNumber) - 1 <= 0 || number <= 0) {

                if (getData.MicroGoodsID == p_id) { localStorage.removeItem(getData.Type); } else { getData = ""; }


            } else {


                getData.ConvertNumber = parseInt(getData.ConvertNumber) - 1;
                getData.sumprice = (parseFloat(price) * parseInt(getData.ConvertNumber)).toFixed(2);
                xxl.xd.setValue(getData.Type, JSON.stringify(getData));
            }
        }
        var cookieStr = "";
        for (var i = 0; i < localStorage.length; i++) {
            strKey = localStorage.key(i);
            if (strKey.substring(0, 5) == "good_") {
                getData = JSON.parse(xxl.xd.getValue(strKey));
                cookieStr += getData.MicroGoodsID + ',' + getData.sumprice + ',' + getData.ConvertNumber + '|';
                $("number_" + p_id).val(getData.ConvertNumber);
                total_num += parseInt(getData.ConvertNumber);
                total_price += parseFloat(getData.sumprice);
            }
        }

    } else {
        return;
    }

    setCookie("goodlist", cookieStr);
    setCookie("goodlist_num", total_num);
    setCookie("goodlist_price", parseFloat(total_price).toFixed(2));
}

//验证商品库存
function GoodsNumber(p_id, a) {
    var num = 0;
    var uid = getCookie("uid");
    $.get("/Service/AjaxService.ashx?Method=GoodsNumber", { "pid": p_id, "uid": uid }, function (text) { num = text; a(num); }, "text");
}


